/*
 * Copyright (C), 2007-2018, 铭师堂教育科技有限公司
 * FileName: MessageServiceImpl.java
 * Author:   zhaoyi
 * Date:     2018年11月21日 下午1:58:10
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.secsbrain.cloudconsole.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.secsbrain.common.utils.DevOpsHttpUtils;
import com.secsbrain.message.dto.MailDto;
import com.secsbrain.message.intf.IMailSendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.secsbrain.cloudconsole.biz.MessageService;

/**
 * 消息相关服务
 *
 * @author zhaoyi
 */
@Service
public class MessageServiceImpl implements MessageService {

	private static final Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

	@Autowired
	IMailSendService mailSendService;

	@Value("${ops.base.url:}")
	private String opsBaseUrl;

	@Autowired
	DevOpsHttpUtils devOpsHttpUtils;
	
	/* (non-Javadoc)
	 * @see MessageService#sendDingMessage(java.util.List, java.lang.String, java.lang.String)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void sendDingMessage(List<String> mobileList, String title,String content) throws Exception {
		Object[] obj={mobileList,title,content};
		logger.debug("发送钉钉消息。。。mobileList:{},title:{},content:{}",obj);

		Assert.notEmpty(mobileList, "发送者列表不能为空");
		Assert.hasText(title, "标题不能为空");
		Assert.hasText(content, "内容不能为空");

		for (String mobile : mobileList){
			try {
				String url=opsBaseUrl+"/api/v1/dingtalk/sendmsg";
				Map<String,Object> params=new HashMap<>();
				params.put("phone",mobile);
				params.put("msg",title+"</br>"+content);
				String result=devOpsHttpUtils.getRequest(url,"",params);
				logger.debug("消息发送结果："+result);
			}catch (Exception e){
				logger.error("",e);
			}

		}
	}

	/* (non-Javadoc)
	 * @see MessageService#sendEmailMessage(java.util.List, java.lang.String, java.lang.String)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void sendEmailMessage(List<String> emailList, String title,String content) throws Exception {
		Object[] obj={emailList,title,content};
		logger.debug("发送邮件消息。。。mailList:{},title:{},content:{}",obj);

		Assert.notEmpty(emailList, "发送者列表不能为空");
		Assert.hasText(title, "标题不能为空");
		Assert.hasText(content, "内容不能为空");

		for (String email:emailList){
			MailDto mailDto=new MailDto();
			mailDto.setEmail(email);
			mailDto.setCheckUrl(content);
			mailDto.setMailType(MailDto.MAILTYPE_NOTICE);
			mailDto.setName(title);
			mailDto.setUsername(title+"</br>"+content);
			mailSendService.sendMail(mailDto);
		}

	}


}
